<?php

namespace App\Store\Controllers\Sysset;

use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Sysset\Predepositgive;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Validate\Sysset\Predepositgive as ValidatePredepositgive;

/**
 * 预存赠送比例
 */
class PredepositgiveController extends Controller
{
    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['store_id', 'money_num', 'points_send', 'money_send', 'account_reward_money']);
        $Validate = new ValidatePredepositgive('add');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        // 组装数据
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $changegoods_data = [
            'cp_id'                    => $cp_id,
            'store_id'                 => $req_data['store_id'],
            'money_num'                => $req_data['money_num'],
            'points_send'              => $req_data['points_send'],
            'money_send'               => $req_data['money_send'],
            'account_reward_money'     => $req_data['account_reward_money'],
            'create_account_id'        => $accountinfo['id'],
            'create_account_real_name' => $accountinfo['real_name'],
            'create_at'                => time(),
        ];

        Predepositgive::create($changegoods_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $list = Predepositgive::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 详情
     */
    public function getone(Request $request)
    {
        $one = Predepositgive::where('id', $request['id'])->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '000', 'msg' => '获取失败']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'store_id', 'money_num', 'points_send', 'money_send', 'account_reward_money']);
        $Validate = new ValidatePredepositgive('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $req_data['update_account_id']        = $accountinfo['id'];
        $req_data['update_account_real_name'] = $accountinfo['real_name'];
        $req_data['update_at']                = time();
        Predepositgive::where('id', $request['id'])->update($req_data);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidatePredepositgive('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $req_data['is_del']                   = 2;
        $req_data['update_account_id']        = $accountinfo['id'];
        $req_data['update_account_real_name'] = $accountinfo['real_name'];
        $req_data['update_at']                = time();

        Predepositgive::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 通过充值金额拿数据
     */
    public function getonebymoney(Request $request)
    {
        $req_data = $request->only(['money_num']);
        $Validate = new ValidatePredepositgive('getonebymoney');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        $store_id = $accountinfo['store_ids'];

        $one = Predepositgive::query()->where([
            'cp_id' => $cp_id,
            'store_id' => $store_id,
            'money_num' => $req_data['money_num']
        ])
            ->orderBy('id','desc')
            ->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '001', 'msg' => '获取失败']);
    }
}
